package com.zg.autoform.web;

import cn.hutool.core.bean.BeanUtil;
import com.aspose.words.Document;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.utils.RT;
import com.utils.Word2PdfUtil;
import com.zg.autoform.util.CustomMultipartFile;
import com.zg.autoform.web.req.*;
import com.zg.common.core.A;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.autoform.*;
import com.zg.common.core.util.JsonUtil;
import com.zg.common.web.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
@Api(tags = {"模板管理"})
public class TmplController {
    private final DiyTmplDao tmplDao;
    private final DiyTmplFileDao tmplFileDao;
    private final MinioService minioService;
    @ApiOperation("模版管理列表")
    @PostMapping("/tmpl/list")
    public R<List<DiyTmpl>> tmplList(@RequestBody @Validated FormIdReq formIdReq) {
        List<DiyTmpl> list = tmplDao.list(
                new LambdaQueryWrapper<DiyTmpl>()
                        .eq(DiyTmpl::getFormId, formIdReq.getFormId())
                        .orderByDesc(DiyTmpl::getCreateTime));
        return R.success(list);
    }
    @ApiOperation("新增模版")
    @PostMapping("/tmpl/add")
    public R tmplAdd(@RequestBody AddTmplReq req) {
        if(tmplDao.exists(new LambdaQueryWrapper<DiyTmpl>()
                .eq(DiyTmpl::getFormId, req.getFormId())
                .eq(DiyTmpl::getName, req.getName()))){
            return R.error("该模版名称已存在");
        }
        DiyTmpl diyTmpl = new DiyTmpl();
        BeanUtil.copyProperties(req,diyTmpl);
        tmplDao.save(diyTmpl);
        return R.success();
    }
    @ApiOperation("删除模版")
    @PostMapping("/tmpl/del")
    public R tmplDel(@RequestBody DelTmplReq tmplReq) {
        tmplDao.removeBatchByIds(tmplReq.getIds());
        return R.success();
    }
    @ApiOperation("查询模板附件列表")
    @PostMapping("/tmplFile/list")
    public R<List<DiyTmplFile>> tmplFilelist(@RequestBody @Validated TmplIdReq tmplIdReq) {
        List<DiyTmplFile> list = tmplFileDao.list(
                new LambdaQueryWrapper<DiyTmplFile>()
                        .eq(DiyTmplFile::getTmplId, tmplIdReq.getTmplId())
                        .orderByDesc(DiyTmplFile::getCreateTime));
        return R.success(list);
    }
    @ApiOperation("新增模版附件")
    @PostMapping("/tmplFile/add")
    public R tmplFileAdd(@RequestParam("type") String type,@RequestParam("tmplId") Long tmplId, @RequestParam("name") String name,  @RequestParam("sort") Integer sort, @RequestParam(value = "file",required = false) MultipartFile file) {
        DiyTmplFile diyTmplFile = new DiyTmplFile();
        try {
            String pdfFilePath = "";
            String filePath = "";
            if(file!=null &&!file.isEmpty())
            {
                //上传正文模板
                filePath = minioService.upload(file);
                File pdfFile;
                if(type.equals("word")){
                    pdfFile = Word2PdfUtil.doc2pdf(new Document(file.getInputStream()), null, null);
                }else {
                    pdfFile = Word2PdfUtil.excel2pdf(file.getInputStream());
                }
                MultipartFile pdfMultipartFile = new CustomMultipartFile(pdfFile, "file.pdf", "file.pdf", "application/pdf");
                pdfFilePath =  minioService.upload(pdfMultipartFile);
                diyTmplFile.setUrl(filePath);
                diyTmplFile.setPdfUrl(pdfFilePath);
                //清理临时文件
                if (pdfFile.exists()) {
                    pdfFile.delete();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        diyTmplFile.setName(name);
        diyTmplFile.setSort(sort);
        diyTmplFile.setType(type);
        diyTmplFile.setTmplId(tmplId);
        tmplFileDao.save(diyTmplFile);
        return R.success();
    }
    @ApiOperation("删除模版附件")
    @PostMapping("/tmplFile/del")
    public R tmplFileDel(@RequestBody @Validated DelTmplFileReq req) {
        tmplFileDao.removeBatchByIds(req.getIds());
        return R.success();
    }

}
